package com.starleague.hexagonal.jpa.appengine;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Embeddable;

import com.starleague.hexagonal.AbstractHexCoordinate;
import com.starleague.hexagonal.IHexCoordinate;

@Embeddable
public class HexCoordinateGae
        extends AbstractHexCoordinate
{
    public HexCoordinateGae(IHexCoordinate hexCoordinate)
    {
        super(hexCoordinate);
    }

    public HexCoordinateGae()
    {
        // Both X and Y being set to Integer.MAX_VALUE means the object is treated as NULL
        super(Integer.MAX_VALUE, Integer.MAX_VALUE);
    }

    @Basic(optional = false)
    @Column(nullable = false)
    @Override
    public int getGridX()
    {
        return super.getGridX();
    }

    @Override
    public void setGridX(int gridX)
    {
        super.setGridX(gridX);
    }

    @Basic(optional = false)
    @Column(nullable = false)
    @Override
    public int getGridY()
    {
        return super.getGridY();
    }

    @Override
    public void setGridY(int gridY)
    {
        super.setGridY(gridY);
    }

    public static HexCoordinateGae toNullable(HexCoordinateGae hexCoordinate)
    {
        // Both X and Y being set to Integer.MAX_VALUE means the object is treated as NULL
        return (hexCoordinate != null)
               && ((hexCoordinate.getGridX() != Integer.MAX_VALUE) || (hexCoordinate.getGridY() != Integer.MAX_VALUE))
                ? hexCoordinate
                : null;
    }

    public static HexCoordinateGae fromNullable(IHexCoordinate hexCoordinate)
    {
        return (hexCoordinate != null) ? new HexCoordinateGae(hexCoordinate) : new HexCoordinateGae();
    }
}
